package jp.michi.makimono;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import android.util.Log;

public class PageLayout {
    protected boolean mHorizontalMode = true;
    protected boolean mLeft2right = true;
    protected int mViewportWidth;
    protected int mViewportHeight;
    protected int mMargin = 1;
    protected int mBorder = 1;
    protected int mCurrentPageIndex = 0;
    protected List<Page> mPages = new ArrayList<Page>();
    protected int mFitWidthHeight = 0;
    public void load(File file) {
        mPages.clear();
        if (file == null || !file.exists()) {
            return;
        } else {
            if (file.isDirectory()) {
                File[] fileList = file.listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File f) {
                        return f.isFile();
                    }
                });
                Arrays.sort(fileList);
                for (File f : fileList) {
                    Page page = Page.newPage(f);
                    Log.v("Page.newPage", String.valueOf(f));
                    if (page != null) {
                        mPages.add(page);
                    }
                }
                if (!mPages.isEmpty()) {
                    mCurrentPageIndex = 0;
                }
            } else {
                load(file.getParentFile());
                if (!mPages.isEmpty()) {
                    Page targetPage = new Page();
                    targetPage.mFile = file;
                    int found = Collections.binarySearch(mPages, targetPage, new Comparator<Page>() {
                        @Override
                        public int compare(Page p1, Page p2) {
                            return p1.mFile.compareTo(p2.mFile);
                        }
                    });
                    if (found >= 0) {
                        mCurrentPageIndex = found;
                    }
                }
            }
        }
    }
    public void show(int pageIndex, int offsetX, int offsetY, int mZoomWidth, int mZoomHeight) {}
    public void fitTo(int viewportWidth, int viewportHeight) {
        for (Page page : mPages) {
            if (viewportWidth > viewportHeight) {
                page.setZoomHeight(viewportHeight);
            } else {
                page.setZoomWidth(viewportWidth);
            }
        }
    }
}
